-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
61 lines (49 loc) · 1.7 KB
/
Solution.java
File metadata and controls
61 lines (49 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import java.util.Scanner;
public class MergeSortedArrays {
static int[] mergeSortedArrays(int[] arr1, int[] arr2) {
int n1 = arr1.length, n2 = arr2.length;
int[] merged = new int[n1 + n2];
int i = 0, j = 0, k = 0;
// Merge both arrays
while (i < n1 && j < n2) {
if (arr1[i] <= arr2[j]) {
merged[k++] = arr1[i++];
} else {
merged[k++] = arr2[j++];
}
}
// Copy remaining elements of arr1
while (i < n1) {
merged[k++] = arr1[i++];
}
// Copy remaining elements of arr2
while (j < n2) {
merged[k++] = arr2[j++];
}
return merged;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of elements in the first sorted array: ");
int n1 = scanner.nextInt();
int[] arr1 = new int[n1];
System.out.println("Enter the elements of the first sorted array:");
for (int i = 0; i < n1; i++) {
arr1[i] = scanner.nextInt();
}
System.out.print("Enter the number of elements in the second sorted array: ");
int n2 = scanner.nextInt();
int[] arr2 = new int[n2];
System.out.println("Enter the elements of the second sorted array:");
for (int i = 0; i < n2; i++) {
arr2[i] = scanner.nextInt();
}
int[] merged = mergeSortedArrays(arr1, arr2);
System.out.print("Merged sorted array: ");
for (int num : merged) {
System.out.print(num + " ");
}
System.out.println();
scanner.close();
}
}